
CROSS_PREFIX := riscv64-linux-gnu-

CC := $(CROSS_PREFIX)gcc
AS := $(CROSS_PREFIX)as
LD := $(CROSS_PREFIX)ld
OBJDUMP := $(CROSS_PREFIX)objdump
OBJCOPY := $(CROSS_PREFIX)objcopy
# LD_FLAGS := -mno-strict-align
# LD_FLAGS += -mabi=lp64
# C_FLAGS := -O2
# LD_FLAGS := -L/CORE/CAD/arm-dev/aarch-gcc/gcc-linaro-7.2.1-2017.11-x86_64_aarch64-elf/aarch64-elf/libc/usr/lib/ -lc
# LD_FLAGS += -lgcc 
C_FLAGS += -march=rv64ifd
# C_FLAGS += -march=rv64g
# C_FLAGS += -march=rv64gc
LD_FLAGS += -nostdlib

# C_FLAGS += -D

objs := start.o

# qemu_args := -plugin /home/long/sysroot/centos7/home/long/qemu-5.0.0-rc1/_build/tests/plugin/libhowvec.so,arg=inline,arg=hint

qemu_args := -plugin /home/long/sysroot/centos7/home/long/qemu-5.0.0-rc1/_build/tests/plugin/libmem-riscv.so

# qemu_args := -plugin /home/long/sysroot/centos7/home/long/qemu-5.0.0-rc1/_build/tests/plugin/libinsn.so

# qemu_args += -singlestep

build: test.elf

run: build
	/home/long/work/qemu-new/qemu-5.0.0-rc1/_build/riscv64-softmmu/qemu-system-riscv64 \
	-machine virt -nographic -smp 1 -m 2048 \
	-nic none \
	-bios test.elf.bin \
	$(qemu_args)

test.elf: $(objs)
	$(LD) $(LD_FLAGS) -T link.lds $^ -o $@
	$(OBJDUMP) -D $@ > $@.asm
	$(OBJCOPY) -O binary $@ $@.bin

clean:
	rm *.o *.elf *.asm *.bin -f

%.o: %.c
	$(CC) $(C_FLAGS) -c $^ -o $@

%.o: %.S
	$(CC) $(C_FLAGS) -c $^ -o $@
